﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace BanmaytinhDAL
{
    public class SQLDataProvider
    {
        static string Connstr= System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
        public static SqlConnection sqlconnection;
        public SQLDataProvider()
        {
            if (sqlconnection == null) { sqlconnection = new SqlConnection(Connstr); }

        }
        public static SqlConnection GetConnection()
        {
            if (sqlconnection.State == ConnectionState.Closed)
            {
                sqlconnection.Open();
                return sqlconnection;
            }
            else
                return sqlconnection;
        }
        private SqlCommand GetCommand(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, GetConnection());
            return cmd;
        }

        // Hàm truy cập csdl
        public DataTable GetData(string sql)
        {
            return GetData(GetCommand(sql));

        }
        public DataTable GetData(SqlCommand cmd)
        {
            try
            {
                if (cmd.Connection == null) { cmd.Connection = GetConnection(); }
                using (DataSet ds = new DataSet())
                {
                    using (SqlDataAdapter da = new SqlDataAdapter())
                    {
                        da.SelectCommand = cmd;
                        da.Fill(ds);
                        return ds.Tables[0];
                    }
                }
            }
            finally
            {

            }
        }

        
        
       
    }
}
